package com.sky.web.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    //依赖注入
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐分页
     *
     * @param setmealPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result findByPage(SetmealPageDTO setmealPageDTO) {
        //1 接收参数

        //2 调用service
        PageBean<SetmealVO> pageBean = setmealService.findByPage(setmealPageDTO);
        //3 返回结果
        return Result.success(pageBean);
    }


    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.categoryId")
    @PostMapping
    public Result save(@RequestBody @Validated(SetmealDTO.Save.class) SetmealDTO setmealDTO) {
        //1 接收参数(json转Java ---> @RequestBody  )

        //2 调用Service
        setmealService.save(setmealDTO);

        //3 返回结果
        return Result.success();
    }


    /**
     * 回显套餐(因为要修改)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")  //请求方式(路径请求)
    public Result findById(@PathVariable("id") Long id) {
        //1 接收参数

        //2 调用service
        SetmealVO setmealVO = setmealService.findById(id);
        //3 返回结果
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping  // 请求参数
    public Result update(@RequestBody @Validated(SetmealDTO.Update.class) SetmealDTO setmealDTO) {
        //1 接收参数 (json(前端) -----(转)---->Java 用@RequestBody)
        //2 调用service
        setmealService.update(setmealDTO);
        //3 返回
        return Result.success();
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        //1 接收参数
        //2 调用service
        setmealService.delete(ids);
        //3 返回结果
        return Result.success();
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable("status") Integer status, Long id) {
        //1 接收参数  (路径参数 @PathVariable)
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        //2 调用Service
        setmealService.startOrStop(setmeal);
        //3 返回结果
        return Result.success();
    }
}
